<template>
  <el-container class="home-container">
    <!-- 头部区域 -->

    <el-header>
      <div>
        <img src="../assets/images/yuantouxiang.jpg" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollape ? '64px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollp">|||</div>
        <!-- 菜单栏只能打开一个 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollape"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + itemss.path"
              v-for="itemss in item.children"
              :key="itemss.id"
              @click="saveNav('/' + itemss.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ itemss.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main> <router-view></router-view> </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src

export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-claim',
        101: 'el-icon-shopping-bag-2',
        102: 'el-icon-tickets',
        145: 'el-icon-edit',
      },
      isCollape: false,
      // 被激活的链接地址
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/mylogin')
    },
    // 获取左边侧边栏的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollp() {
      this.isCollape = !this.isCollape
    },

    // 保存链接的高亮状态
    saveNav(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>

<style lang="scss" scoped>
.el-header {
  height: 100px !important;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  user-select: none;
  & div {
    display: flex;
    align-items: center;
  }
  & div > img {
    width: 100px;
    height: 100px;
    border-radius: 50px;
    margin-right: 75px;
    cursor: pointer;
  }
  & div > span {
    font-size: 32px;
    color: white;
    margin-left: 20px;
    user-select: none;
  }
}
.el-aside {
  background-color: #333744;
  user-select: none;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  user-select: none;
  background-color: #eaedf1;
}
.home-container {
  user-select: none;
  height: 100vh;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
